<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户账号" prop="accountNo">
      <el-input v-model="dataForm.accountNo" placeholder="用户账号"></el-input>
    </el-form-item>
    <el-form-item label="会员头像" prop="avatar">
      <el-input v-model="dataForm.avatar" placeholder="会员头像"></el-input>
    </el-form-item>
    <el-form-item label="会员状态：1正常，2锁定，3黑名单，4删除, 5封号" prop="status">
      <el-input v-model="dataForm.status" placeholder="会员状态：1正常，2锁定，3黑名单，4删除, 5封号"></el-input>
    </el-form-item>
    <el-form-item label="姓名（加密）" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名（加密）"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="手机号（加密）" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机号（加密）"></el-input>
    </el-form-item>
    <el-form-item label="应用ID" prop="appId">
      <el-input v-model="dataForm.appId" placeholder="应用ID"></el-input>
    </el-form-item>
    <el-form-item label="" prop="creator">
      <el-input v-model="dataForm.creator" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updater">
      <el-input v-model="dataForm.updater" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="password">
      <el-input v-model="dataForm.password" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="gender">
      <el-input v-model="dataForm.gender" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="身份证" prop="identityCardNumber">
      <el-input v-model="dataForm.identityCardNumber" placeholder="身份证"></el-input>
    </el-form-item>
    <el-form-item label="身份证地址" prop="identityCardAddress">
      <el-input v-model="dataForm.identityCardAddress" placeholder="身份证地址"></el-input>
    </el-form-item>
    <el-form-item label="盐" prop="salt">
      <el-input v-model="dataForm.salt" placeholder="盐"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          accountNo: '',
          avatar: '',
          status: '',
          name: '',
          nickname: '',
          mobile: '',
          appId: '',
          creator: '',
          createTime: '',
          updater: '',
          updateTime: '',
          password: '',
          gender: '',
          identityCardNumber: '',
          identityCardAddress: '',
          salt: ''
        },
        dataRule: {
          accountNo: [
            { required: true, message: '用户账号不能为空', trigger: 'blur' }
          ],
          avatar: [
            { required: true, message: '会员头像不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '会员状态：1正常，2锁定，3黑名单，4删除, 5封号不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '姓名（加密）不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号（加密）不能为空', trigger: 'blur' }
          ],
          appId: [
            { required: true, message: '应用ID不能为空', trigger: 'blur' }
          ],
          creator: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updater: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          identityCardNumber: [
            { required: true, message: '身份证不能为空', trigger: 'blur' }
          ],
          identityCardAddress: [
            { required: true, message: '身份证地址不能为空', trigger: 'blur' }
          ],
          salt: [
            { required: true, message: '盐不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/disk/member/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.accountNo = data.member.accountNo
                this.dataForm.avatar = data.member.avatar
                this.dataForm.status = data.member.status
                this.dataForm.name = data.member.name
                this.dataForm.nickname = data.member.nickname
                this.dataForm.mobile = data.member.mobile
                this.dataForm.appId = data.member.appId
                this.dataForm.creator = data.member.creator
                this.dataForm.createTime = data.member.createTime
                this.dataForm.updater = data.member.updater
                this.dataForm.updateTime = data.member.updateTime
                this.dataForm.password = data.member.password
                this.dataForm.gender = data.member.gender
                this.dataForm.identityCardNumber = data.member.identityCardNumber
                this.dataForm.identityCardAddress = data.member.identityCardAddress
                this.dataForm.salt = data.member.salt
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/disk/member/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'accountNo': this.dataForm.accountNo,
                'avatar': this.dataForm.avatar,
                'status': this.dataForm.status,
                'name': this.dataForm.name,
                'nickname': this.dataForm.nickname,
                'mobile': this.dataForm.mobile,
                'appId': this.dataForm.appId,
                'creator': this.dataForm.creator,
                'createTime': this.dataForm.createTime,
                'updater': this.dataForm.updater,
                'updateTime': this.dataForm.updateTime,
                'password': this.dataForm.password,
                'gender': this.dataForm.gender,
                'identityCardNumber': this.dataForm.identityCardNumber,
                'identityCardAddress': this.dataForm.identityCardAddress,
                'salt': this.dataForm.salt
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
